//
//  ShowViewController.swift
//  DHLive
//
//  Created by Mac on 2021/6/28.
//

import UIKit

class ShowViewController: BaseViewController {

    lazy var titles: Array = {
       ["关注", "热门", "附近的人"]
    }()
    lazy var vcNames: Array = {
        ["FocusViewController",
         "HotViewController",
         "NearViewController"]
    }()
    
    lazy var topView: ShowTopView = {
        ShowTopView(frame: CGRect(x: 0, y: 0, width: kScreenWidth * 0.6, height: 44), titles: titles)
    }()
    
    var scrollView: UIScrollView = UIScrollView()
    private var startOffsetX: CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "global_search"), style: .done, target: self, action: #selector(leftItemAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "title_button_more"), style: .done, target: self, action: #selector(rightItemAction))
        
        topView.tapBlock = {
            idx in
            let point = CGPoint(x: idx * Int(kScreenWidth), y: 0)
            self.scrollView.setContentOffset(point, animated: true)
        }
        navigationItem.titleView = topView
        
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight-kHeight_NavBar-kHeight_TabBar))
        scrollView.contentSize = CGSize(width: kScreenWidth * CGFloat(titles.count), height: 0)
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.delegate = self
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentOffset = CGPoint(x: kScreenWidth, y: 0)
        view.addSubview(scrollView)
        
        for vcName in vcNames {
            addChild(vcName.dh.toController!)
        }
        scrollViewDidEndScrollingAnimation(scrollView)
    }

}

extension ShowViewController: UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let idx: Int = Int(scrollView.contentOffset.x / kScreenWidth)
        
        topView.lineIndex = idx
        topView.setLineView(width: idx)
        let vc = children[idx]
        if vc.isViewLoaded {
            return
        }
        vc.view.frame = CGRect(x: scrollView.contentOffset.x, y: 0, width: kScreenWidth, height: kScreenHeight-kHeight_TabBar-kHeight_NavBar)
        scrollView.addSubview(vc.view)
        
    }
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        scrollViewDidEndDecelerating(scrollView)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
      
        let currentOffsetX = scrollView.contentOffset.x
        if currentOffsetX > startOffsetX {
            //print("右滑")
            topView.setLineView(offset: currentOffsetX-startOffsetX, isRight: true)
        }else {
            //print("左滑")
            topView.setLineView(offset: startOffsetX-currentOffsetX, isRight: false)
        }
        
        
    }
    //开始拖拽，判断滚动方向
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX = scrollView.contentOffset.x
        print("开始滚动", startOffsetX)
    }
}

extension ShowViewController {
    @objc func leftItemAction() {
        print("left")
    }
    @objc func rightItemAction() {
        print("right")
    }
}
